package by.yakusekmaxim.bookgid.webapp.page.admin.user;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.UserAccountFilter;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount_;
import by.yakusekmaxim.bookgid.datamodel.enums.UserRole;
import by.yakusekmaxim.bookgid.datamodel.enums.UserStatus;
import by.yakusekmaxim.bookgid.services.UserAccountService;

public class UserListPanel extends Panel {

	@Inject
	private UserAccountService userAccountService;

	public UserListPanel(String id, UserAccountFilter filter) {
		super(id);
		setOutputMarkupId(true);
		UserAccounttDataProvider userAccounttDataProvider = new UserAccounttDataProvider(
				filter);

		DataView<UserAccount> dataView = new DataView<UserAccount>("list",
				userAccounttDataProvider, 10) {
			@Override
			protected void populateItem(Item<UserAccount> item) {
				final UserAccount userAccount = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("lastName"));
				item.add(new Label("firstName"));
				item.add(new Label("middleName"));
				item.add(new Label("created"));
				item.add(new Label("status", getStatus(userAccount)));
				item.add(new Label("role", getRole(userAccount)));
				item.add(new Link<Object>("edit") {
					@Override
					public void onClick() {
						setResponsePage(new EditUserPage(userAccount));
					}
				});
			}

		};
		add(dataView);

		add(new PagingNavigator("paging", dataView));

		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>("sortByID",
				UserAccount_.id, userAccounttDataProvider));
		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>(
				"sortByLastname", UserAccount_.lastName,
				userAccounttDataProvider));
		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>(
				"sortByFirstname", UserAccount_.firstName,
				userAccounttDataProvider));
		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>(
				"sortByMiddlename", UserAccount_.middleName,
				userAccounttDataProvider));
		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>(
				"sortByCreated", UserAccount_.created, userAccounttDataProvider));
		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>(
				"sortByStatus", UserAccount_.status, userAccounttDataProvider));
		add(new OrderByBorder<SingularAttribute<UserAccount, ?>>("sortByRole",
				UserAccount_.role, userAccounttDataProvider));

	}

	private class UserAccounttDataProvider
			extends
			SortableDataProvider<UserAccount, SingularAttribute<UserAccount, ?>> {

		private UserAccountFilter filter;

		public UserAccounttDataProvider(UserAccountFilter filter) {
			super();
			setSort(UserAccount_.id, SortOrder.ASCENDING);
			this.filter = filter;

		}

		@Override
		public Iterator<? extends UserAccount> iterator(long first, long count) {

			SingularAttribute<UserAccount, ?> sortParam = getSort()
					.getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(
					sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);

			if (filter.getEnabled()) {
				return userAccountService.getAllUserAccount(sortParam,
						ascending, (int) first, (int) count, filter).iterator();
			}
			return userAccountService.getAllUserAccount(sortParam, ascending,
					(int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			if (filter.getEnabled()) {
				return userAccountService.getCount(filter.getAttribute(),
						filter.getValue());
			}
			return userAccountService.getCount();
		}

		@Override
		public IModel<UserAccount> model(UserAccount userAccount) {
			return new CompoundPropertyModel<UserAccount>(userAccount);
		}
	}

	private ResourceModel getRole(UserAccount userAccount) {
		UserRole role = userAccount.getRole();

		if (role == UserRole.userrole_admin) {
			return new ResourceModel("p.admin.user.role.admin");
		}

		if (role == UserRole.userrole_editor) {
			return new ResourceModel("p.admin.user.role.editor");
		}

		if (role == UserRole.userrole_user) {
			return new ResourceModel("p.admin.user.role.user");
		}

		return null;
	}

	private ResourceModel getStatus(UserAccount userAccount) {
		UserStatus status = userAccount.getStatus();

		if (status == UserStatus.userstatus_active) {
			return new ResourceModel("p.admin.user.status.active");
		}

		if (status == UserStatus.userstatus_locked) {
			return new ResourceModel("p.admin.user.status.locked");
		}

		if (status == UserStatus.userstatus_inactive) {
			return new ResourceModel("p.admin.user.status.inactive");
		}

		return null;
	}
}
